﻿
using Models.Common;
using Models.Device;
using Models.TaskPlain;
using NetTaste;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;

using UtilsORM.MySqlData;
using static Models.Common.EnumDeviceState;
using UtilsBase;
using GatewayServerService.CloudCenter;

namespace GatewayServerService.Device
{
    /// <summary>
    /// 485继电器16路，中盛
    /// </summary>
    public class ServiceDeviceRS4851034_JDQ16
    {
        //回调获获状态,为了避免误差，查状态和执行，都返回了 当前开关状态
        public void callBackUpdate(MQReqModel<object> httpReqModel)
        {
            try
            {
                ModelReceverData modelDeviceData = httpReqModel.toServerData;
                CommandLogModel commandLogModel = httpReqModel.toGatewayData;

                int status = 1;// int.Parse(httpReqModel.respCode);
                string key = "";
                if (httpReqModel.respCode == ResponseCode.SUCCESS)
                {
                    ModelCommandAgreement modelCommandAgreement = JsonConvert.DeserializeObject<ModelCommandAgreement>(commandLogModel.parameter);
                    ModelCommandAgreementParameter modelCommandAgreementParameter = UtilsObject.DynamicToEntity<ModelCommandAgreementParameter>(modelCommandAgreement.agreementParameter);
                    List<ModelCommandAgreementParameterCommand> commands = UtilsObject.DynamicToEntity<List<ModelCommandAgreementParameterCommand>>(modelCommandAgreement.agreementParameter.command);
                    ModelCommandAgreementParameterCommand command = commands.Where(c => c.key == commandLogModel.key).FirstOrDefault();

                    if (string.IsNullOrWhiteSpace(modelDeviceData.commandResults[0].value))
                    {
                        return;
                    }

                    //if (modelDeviceData.commandResults[0].value.Substring(0, 2) != commandLogModel.content.Substring(0, 2))
                    //{
                    //    return;
                    //}


                    string result = modelDeviceData.commandResults[0].value;

                    if (commandLogModel.key == "open")
                    {
                        //开
                        key = "open";
                    }
                    else if (commandLogModel.key == "close")
                    {
                        //关
                        key = "close";
                    }
                    else if (commandLogModel.key == "check_state")
                    {
                        string val = commands.Where(c => c.key == "open").FirstOrDefault().returnResult;
                        if (val.Contains(result))
                        {
                            //开
                            key = "open";
                            commandLogModel.name = "打开";
                        }
                        else
                        {
                            //关
                            key = "close";
                            commandLogModel.name = "关闭";
                        }
                    }
                    ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
                    log.writeSensorlog(commandLogModel.d_guid, "status", key, commandLogModel.name);
                }
                //else
                //{
                //    status = (int)Device_State.error;
                //    //更新设备状态，设备状态
                //    new ServiceCloudDeviceLiveState().setDeviceLiveState(commandLogModel.d_guid, status, httpReqModel.respMsg);

                //}

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

    }

}
